package org.jfk.core.web.taglib;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;

import org.jfk.sys.security.Urls;

import com.jfinal.kit.StrKit;
import com.jfinal.plugin.ehcache.CacheKit;

public class NavBarTag extends AbstractTag {

	public final static String CACHE_NAME_NAVBARTAG = "NavBarTag";
	
	
	public String label ;
	public String getLabel() {
		return label;
	}
	public void setLabel(String label) {
		this.label = label;
	}
	@Override
	public int doEndTag() throws JspException {
		String returnStr="";

		try {
			String strUrl = super.getUrl();

			
			if (strUrl != null) {
				
				returnStr = CacheKit.get(CACHE_NAME_NAVBARTAG, strUrl);
				
				if (StrKit.isBlank(returnStr)){
					Urls url = Urls.dao.findByUrl(strUrl);
					if (url != null) {
						if (label.equals("name")) {
							returnStr = url.getStr("name");
						} else if (label.equals("description")) {
							returnStr = url.getStr("description");
						}
					}else{
						int i = strUrl.lastIndexOf("/");
						if (i != -1){
							strUrl = strUrl.substring(0, i);
						}
						url = Urls.dao.findByUrl(strUrl);
						if (url != null){
							if (label.equals("name")) {
								returnStr = url.getStr("name");
							} else if (label.equals("description")) {
								returnStr = url.getStr("description");
							}
						}
					}
					CacheKit.put(CACHE_NAME_NAVBARTAG, strUrl, returnStr);

				}

				
			}

			pageContext.getOut().print(returnStr);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return EVAL_PAGE;
	}

}
